package com.joy.web.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.joy.common.exception.JsonException;
import com.joy.common.exception.PageException;
import com.joy.common.model.ApiResponse;
import com.joy.common.model.DateTablePage;
import com.joy.web.admin.service.IRoleService;
import com.joy.web.admin.service.IUserRoleService;
import com.joy.web.admin.service.IUserService;
import com.joy.common.enums.Status;
import com.joy.common.model.vo.UserInfoVO;
import com.joy.web.admin.entity.Role;
import com.joy.web.base.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yjzhao
 * @since 2019-09-09
 */
@Slf4j
@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController {


    @Resource
    private IUserService userService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IUserRoleService userRoleService;

    /**
     * 用户管理初始化页面
     * @return
     */
    @GetMapping(value = {"", "/", "/index" })
    public String index() {
        return "pages/admin/user/index";
    }

    /**
     * 查询集合
     * @return
     */
    @GetMapping(value = { "/pagination" })
    @ResponseBody
    public DateTablePage pagination(UserInfoVO userInfoVO) {
        try {
            IPage<UserInfoVO> rtnPage = userService.getUserInfoList(userInfoVO, this.getDataTablePage());
            return getDataTableRtn(rtnPage);
        } catch (Exception e) {
            log.info("/admin/user/pagination MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "获取人员集合报错了");
        }

    }

    /**
     * 编辑
     * @return
     */
    @GetMapping(value = { "/edit/{userId}" })
    public String edit(@PathVariable(value="userId") String userId, ModelMap map) {

        try {
            // 根据 userId 获取 用户详情
            UserInfoVO userInfoVO = userService.findUserInfoByUserId(userId);
            //获取用户 已选定角色列表
            List<String> roleIdList = userRoleService.findRoleIdListByUserId(userId);
            // 可供选择角色列表
            List<Role> roleList = roleService.findAllRoleList();

            //传至前端
            map.put("user", userInfoVO);
            map.put("roleList", roleList);
            map.put("roleIdList", roleIdList);
            //用户编辑页
            return "pages/admin/user/edit";
        } catch (Exception e) {
            log.info("/admin/user/edit MyException:");
            e.printStackTrace();
            throw new PageException(Status.UNKNOWN_ERROR, "获取人员信息报错了");
        }
    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/update" })
    @ResponseBody
    public ApiResponse update(UserInfoVO userInfoVO) {
        try {
            //更新用户详情
            boolean upSucc = userService.updateUserBaseInfoAndRoles(userInfoVO);
            userService.removeUserAuthorization();
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/user/update MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "更新人员信息报错了");
        }

    }

    /**
     * 重置密码
     * @return
     */
    @PostMapping(value = { "/resetPwd" })
    @ResponseBody
    public ApiResponse resetPwd(String userId) {
        try {
            //更新用户详情
            boolean upSucc = userService.resetPwd(userId);
            return ApiResponse.ofSuccess("重置密码成功。");
        } catch (Exception e) {
            log.info("/admin/user/resetPwd MyException:");
            e.printStackTrace();
            throw new JsonException(Status.RESET_PWD_ERROR, "密码重置失败");
        }
    }
}
